package docoffice.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import docoffice.doss.Patient;
import docoffice.doss.Patient.insuranceCarrier;

public class AddUser extends JPanel {
	
	private JLabel title, blank, username, name, pass, blank2;
	
	private JLabel userType;
	
	private JTextField userField, nameField, passField, typeField;
	
	private JButton add;
	
	public AddUser() {
		title = new JLabel("Create a new User");
		username = new JLabel("Enter new username");
		userField = new JTextField(25);
		name = new JLabel("Enter name (First Last)");
		nameField = new JTextField(25);
		pass = new JLabel("Enter temporary password");
		passField = new JTextField(25);
		userType = new JLabel("Enter usertype");
		typeField = new JTextField(25);
		
		blank = new JLabel();
		blank2 = new JLabel();
		
		add = new JButton("Create New User");
		
		setLayout(new GridLayout(6, 2, 15, 10));
		add(title);
		add(blank);
		add(username);
		add(userField);
		add(name);
		add(nameField);
		add(pass);
		add(passField);
		add(userType);
		add(typeField);
		add(blank2);
		add(add);
		
		TextListener testList = new TextListener();
		userField.addActionListener(testList);
		nameField.addActionListener(testList);
		passField.addActionListener(testList);
		typeField.addActionListener(testList);
		add.addActionListener(new AddListener());
		
		setPreferredSize(new Dimension(500, 200));
		setBackground(Color.WHITE);
	}

	private class TextListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			String userText = userField.getText();
			String nameText = nameField.getText();
			String passText = passField.getText();
			String typeText = typeField.getText();
		}
	}
	
	private class AddListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			int value = JOptionPane.showConfirmDialog(null, "Confirm creating a new user?",
					"Confirm", JOptionPane.YES_NO_OPTION);
			if (value == JOptionPane.YES_OPTION) {
				String userText = userField.getText();
				String nameText = nameField.getText();
				String passText = passField.getText();
				Patient patient = new Patient(nameText);
				patient.setUsername(userText);
				patient.setPassword(passText);
				patient.createUser();
				patient.setInsuranceCarrier(insuranceCarrier.NONE);
				patient.createPatient();
				System.out.println("yes");
				removeAll();
				revalidate();
				repaint();
				add(new LoginPanel());
			}
		}
	}
}
